package com.fzshang.shopping.wx.controller;


import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.fzshang.shopping.commen.bean.*;
import com.fzshang.shopping.commen.bean.layuiBean.ResultPage;
import com.fzshang.shopping.commen.bean.layuiBean.VoPage;
import com.fzshang.shopping.commen.controller.BaseController;
import com.fzshang.shopping.commen.entity.ApiResult;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * <p>
 * 商品 前端控制器
 * </p>
 *
 * @author 冯政尚
 * @since 2020-03-08
 */
@Controller
@RequestMapping("wx/good")
public class WXGoodController extends BaseController {

    @RequestMapping(value = "goodDetails.html")
    public String good(Model model,Long id,HttpServletRequest request){
        //查询商品信息
        Good good = goodService.getById(id);
        model.addAttribute("good",good);
        //查询商品图片
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("good_id",good.getId());
        List<GoodImage> goodImages = goodImageService.list(queryWrapper);
        model.addAttribute("goodImages",goodImages);
        model.addAttribute("goodImages",goodImages);
        //查询商品第一张图片
        model.addAttribute("imageOne",goodImages.get(0).getUrl());
        //查询商品详情
        QueryWrapper queryWrapper1 = new QueryWrapper();
        queryWrapper1.eq("good_id",good.getId());
        List<GoodDetails> goodDetails = goodDetailsService.list(queryWrapper1);
        model.addAttribute("goodDetails",goodDetails);
        //查询商品评论
        QueryWrapper queryWrapper2 = new QueryWrapper();
        queryWrapper2.eq("good_id",id);
        List<Comment> comments = commentService.list(queryWrapper2);
        if(comments.size()==0){
            model.addAttribute("size",0);
        }else{
            model.addAttribute("size",1);
            for (int i = 0; i < comments.size(); i++) {
                Comment comment = comments.get(comments.size()-1);
                String stringPhone = comment.getPhone().substring(0,3);
                String stringPhone1 = comment.getPhone().substring(6,10);
                comment.setStringPhone(stringPhone+"****"+stringPhone1);
                model.addAttribute("comment",comment);
            }
        }
        model.addAttribute("goodId",id);
        model.addAttribute("user",getWxUser(request));
        //查询颜色
        QueryWrapper queryWrapper3 = new QueryWrapper();
        queryWrapper3.eq("good_id",id);
        List<GoodSize> goodSizes = goodSizeService.list(queryWrapper3);
        model.addAttribute("goodSizes",goodSizes);


        return "wx/index/goodDetails";
    }

    /**
     * @return 首页查询商品
     */
    @RequestMapping(value = "selectGood")
    @ResponseBody
    public ApiResult selectGood(){
        QueryWrapper queryWrapper1 = new QueryWrapper();
        queryWrapper1.eq("recommend",1);
        queryWrapper1.eq("businesses",1);
        List<Good> goods = goodService.list(queryWrapper1);
        for (int i = 0; i < goods.size(); i++) {
            QueryWrapper queryWrapper = new QueryWrapper();
            queryWrapper.eq("good_id",goods.get(i).getId());
            List<GoodImage> goodImages = goodImageService.list(queryWrapper);
            goods.get(i).setGoodUrl(goodImages.get(0).getUrl());
        }
        return ApiResult.ok(goods);
    }

    /**
     * @return 查询必推好货
     */
    @RequestMapping(value = "selectState")
    @ResponseBody
    public ApiResult selectState(){
        QueryWrapper   queryWrapper = new QueryWrapper();
        queryWrapper.eq("state",1);
        return ApiResult.ok(goodService.getOne(queryWrapper));
    }
    /**
     * @return 查询冰点秒杀
     */
    @RequestMapping(value = "selectPoint")
    @ResponseBody
    public ApiResult selectPoint(){
        QueryWrapper   queryWrapper = new QueryWrapper();
        queryWrapper.eq("state",2);
        return ApiResult.ok(goodService.getOne(queryWrapper));
    }


    /**
     * @return商品管理 上架与下架
     */
    @RequestMapping(value = "updateBusinesses")
    @ResponseBody
    public ApiResult updateBusinesses(Long id){
        Good good = goodService.getById(id);
        if(good.getBusinesses()==1){
            good.setBusinesses(2);
            if(goodService.updateById(good)){
                return ApiResult.ok();
            }
        }else{
            good.setBusinesses(1);
            if(goodService.updateById(good)){
                return ApiResult.ok();
            }
        }
         return ApiResult.fail();
    }

    /**
     * @param model
     * @param id
     * @return 跳转商品
     */
    @RequestMapping(value = "goodList.html")
    public String goodList(Model model,Long id){
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("commodity_id",id);
        List<Good> goods = goodService.list(queryWrapper);
        if(goods.size()==0){
            model.addAttribute("size",0);
        }else{
            model.addAttribute("size",1);
            model.addAttribute("good",goods);
            for (int i = 0; i < goods.size(); i++) {
                QueryWrapper queryWrapper1 = new QueryWrapper();
                queryWrapper1.eq("good_id",goods.get(i).getId());
                List<GoodImage> goodImages = goodImageService.list(queryWrapper1);
                goods.get(i).setGoodUrl(goodImages.get(0).getUrl());
            }
        }

        return "wx/index/goodList";
    }

}
